package com.gome.axwl.aoc.openApi.api.contract;

import com.gome.axwl.aoc.common.util.ErrorCode;
import com.gome.axwl.aoc.entity.dto.PageResultDto;
import com.gome.axwl.aoc.entity.exception.ZQException;
import com.gome.axwl.aoc.entity.po.ContractPo;
import com.gome.axwl.aoc.entity.po.EmptyContractResponse;
import com.gome.axwl.aoc.facade.ContractFacade;
import com.gome.common.utils.StringUtil;
import com.gome.commons.core.ResultData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 *
 * 项目名称：gome-axwl-op-system-web
 * 类名称：DeviceController
 * 类描述：
 * 创建人：chengh
 * 创建时间：2018年5月4日11:00:12
 * @version
 */
@Api(tags="合同")
@RestController
@RequestMapping(value = "/contract")
public class ContractController {

    private final static Logger logger = LoggerFactory.getLogger(ContractController.class);

    @Autowired
    private ContractFacade contractFacade;


    @ApiOperation(value = "分页查询合同信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "contractName",    value = "合同名称", paramType = "query",dataType="String"),
            @ApiImplicitParam(name = "carrierName",    value = "承运商名称", paramType = "query",dataType="String"),
            @ApiImplicitParam(name = "carrierPhone",    value = "合同名称", paramType = "query",dataType="String"),
            @ApiImplicitParam(name = "startTime",       value = "开始时间", paramType = "query",dataType="Date"),
            @ApiImplicitParam(name = "endTime",         value = "结束时间", paramType = "query",dataType="Date"),
            @ApiImplicitParam(name = "pageNum",         value = "当前页", paramType = "query",dataType="int"),
            @ApiImplicitParam(name = "pageSize",        value = "当前页条数", paramType = "query",dataType="int")
    })
    @RequestMapping(value = "/queryContract",method = RequestMethod.GET)
    public ResultData<PageResultDto<ContractPo>> queryContract(String contractName,String carrierPhone,String carrierName, Date startTime,
                                                               Date endTime, Integer pageNum, Integer pageSize){
        if(StringUtil.isBlank(carrierPhone)){
            return ResultData.newResultData(ErrorCode.PARAM_IS_NULL,"carrierPhone 不能为空");
        }
        return ResultData.newResultData(contractFacade.queryContract(contractName, null,carrierPhone,carrierName,null, startTime, endTime, pageNum, pageSize));
    }

    @ApiOperation(value = "获取空白合同详情图片等")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "applyInfoId", value = "申请单号id", paramType = "query",dataType="int")
    })
    @RequestMapping(value = "/getEmptyContract",method = RequestMethod.GET)
    public ResultData<EmptyContractResponse> getEmptyContract(Integer applyInfoId){
        if(applyInfoId == null){
            return ResultData.newResultData(com.gome.axwl.aoc.common.util.ErrorCode.PARAM_IS_NULL, com.gome.axwl.aoc.common.util.ErrorCode.PARAM_IS_NULL_MSG);
        }
        try {
            return ResultData.newResultData(contractFacade.getEmptyContract(applyInfoId));
        } catch (ZQException e) {
            logger.debug("获取空白合同详情图片失败，失败原因[ {} ]",e.getMessage());
            return ResultData.newResultData(e.getCode(), e.getMessage());
        }
    }

    @ApiOperation(value = "服务商签署合同")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "applyInfoId", value = "申请单号id", paramType = "form",dataType="int"),
            @ApiImplicitParam(name = "signName",    value = "签署人姓名", paramType = "form",dataType="String"),
            @ApiImplicitParam(name = "signJson",    value = "签好的json", paramType = "form",dataType="String")
    })
    @RequestMapping(value = "/signByCarrier",method = RequestMethod.POST)
    public ResultData<Integer> signByCarrier(Integer applyInfoId, String signName, String signJson){
        if(applyInfoId == null || StringUtil.isBlank(signName) || StringUtil.isBlank(signJson)){
            return ResultData.newResultData(ErrorCode.PARAM_IS_NULL, ErrorCode.PARAM_IS_NULL_MSG);
        }
        try {
            return ResultData.newResultData(contractFacade.signByCarrier(applyInfoId, signName, signJson));
        } catch (ZQException e) {
            logger.debug("服务商签署合同失败，失败原因[ {} ]",e.getMessage());
            return ResultData.newResultData(e.getCode(), e.getMessage());
        }
    }
    @ApiOperation(value = "获取合同详情图片")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "contractNo", value = "合同编码", paramType = "query",dataType="String")
    })
    @RequestMapping(value = "/getDetail",method = RequestMethod.GET)
    public ResultData<List<String>> getDetail(String contractNo){
        if(StringUtil.isBlank(contractNo)){
            return ResultData.newResultData(ErrorCode.PARAM_IS_NULL,ErrorCode.PARAM_IS_NULL_MSG);
        }
        try {
            return ResultData.newResultData(contractFacade.getDetail(contractNo));
        } catch (ZQException e) {
            logger.debug("获取合同详情图片失败，失败原因[ {} ]",e.getMessage());
            return ResultData.newResultData(e.getCode(), e.getMessage());
        }
    }

    @ApiOperation(value = "下载合同")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "contractNo", value = "合同编码", paramType = "query",dataType="String")
    })
    @RequestMapping(value = "/download",method = RequestMethod.GET)
    public ResultData<List<String>> download(String contractNo){
        if(StringUtil.isBlank(contractNo)){
            return ResultData.newResultData(ErrorCode.PARAM_IS_NULL,ErrorCode.PARAM_IS_NULL_MSG);
        }
        try {
            return ResultData.newResultData(contractFacade.download(contractNo));
        } catch (ZQException e) {
            logger.debug("下载合同失败，失败原因[ {} ]",e.getMessage());
            return ResultData.newResultData(e.getCode(), e.getMessage());
        }
    }
}
